﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;

namespace BusinessLogic
{
    public static class ChiTietHDBanLe
    {
        static SqlCommand cmd;
        static ArrayList arr;
        static SqlDataReader dr;
        static SqlConnection cn;

        #region Selct khách hàng
        public static Entities.ChiTietHDBanle[] Select()
        {
            Entities.ChiTietHDBanle[] list = null;
            try
            {
                string sql = Common.Constants.ChiTietHDBanLe.Select;
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.ChiTietHDBanle chitiethdbanle = new Entities.ChiTietHDBanle();
                    chitiethdbanle.MaHDBanLe = dr[Common.Constants.ChiTietHDBanLe.MaHDBanLe].ToString();
                    chitiethdbanle.MaHangHoa = dr[Common.Constants.ChiTietHDBanLe.MaHangHoa].ToString();
                    chitiethdbanle.TenHangHoa = dr[Common.Constants.ChiTietHDBanLe.TenHangHoa].ToString();
                    chitiethdbanle.SoLuong = int.Parse(dr[Common.Constants.ChiTietHDBanLe.SoLuong].ToString());
                    chitiethdbanle.SoLuongConLai = dr[Common.Constants.ChiTietHDBanLe.SoLuongConLai].ToString();
                    chitiethdbanle.DonGia = (dr[Common.Constants.ChiTietHDBanLe.DonGia].ToString());
                    chitiethdbanle.ThanhTien = (dr[Common.Constants.ChiTietHDBanLe.ThanhTien].ToString());
                    chitiethdbanle.ThueGTGT = (dr[Common.Constants.ChiTietHDBanLe.ThueGTGT].ToString());
                    chitiethdbanle.PhanTramChietKhau = dr[Common.Constants.ChiTietHDBanLe.PhanTramChietKhau].ToString();
                    chitiethdbanle.GhiChu = dr[Common.Constants.ChiTietHDBanLe.GhiChu].ToString();
                    chitiethdbanle.Deleted = bool.Parse(dr[Common.Constants.ChiTietHDBanLe.Deleted].ToString());
                    arr.Add(chitiethdbanle);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = new Entities.ChiTietHDBanle[0];
                }
                else
                {
                    list = new Entities.ChiTietHDBanle[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.ChiTietHDBanle)arr[i];
                    }
                }
                return list;
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return new Entities.ChiTietHDBanle[0]; }
            finally
            {
                cmd.Connection.Dispose();
                cn.Close();
            }

        }
        #endregion

        #region insert update
        public static int InsertUpdate(Entities.ChiTietHDBanle chitiethdbanle)
        {
            int rt = 0;
            try
            {
                string sql = Common.Constants.ChiTietHDBanLe.InsertUpdate;
                cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(Common.Constants.ChiTietHDBanLe.HanhDong, SqlDbType.NVarChar, 50).Value = chitiethdbanle.HanhDong;
                cmd.Parameters.Add(Common.Constants.ChiTietHDBanLe.MaKho, SqlDbType.VarChar).Value = chitiethdbanle.MaKho;
                cmd.Parameters.Add(Common.Constants.ChiTietHDBanLe.MaHDBanLe, SqlDbType.VarChar).Value = chitiethdbanle.MaHDBanLe;
                cmd.Parameters.Add(Common.Constants.ChiTietHDBanLe.MaHangHoa, SqlDbType.VarChar).Value = chitiethdbanle.MaHangHoa;
                cmd.Parameters.Add(Common.Constants.ChiTietHDBanLe.TenHangHoa, SqlDbType.NVarChar).Value = chitiethdbanle.TenHangHoa;
                cmd.Parameters.Add(Common.Constants.ChiTietHDBanLe.SoLuong, SqlDbType.Float).Value = chitiethdbanle.SoLuong;
                cmd.Parameters.Add(Common.Constants.ChiTietHDBanLe.DonGia, SqlDbType.Float).Value = chitiethdbanle.DonGia;
                cmd.Parameters.Add(Common.Constants.ChiTietHDBanLe.PhanTramChietKhau, SqlDbType.Float).Value = chitiethdbanle.PhanTramChietKhau;
                cmd.Parameters.Add(Common.Constants.ChiTietHDBanLe.ThueGTGT, SqlDbType.Float).Value = chitiethdbanle.ThueGTGT;
                cmd.Parameters.Add(Common.Constants.ChiTietHDBanLe.ThanhTien, SqlDbType.Float).Value = chitiethdbanle.ThanhTien;
                cmd.Parameters.Add(Common.Constants.ChiTietHDBanLe.SoLuongConLai, SqlDbType.Float).Value = chitiethdbanle.SoLuongConLai;
                cmd.Parameters.Add(Common.Constants.ChiTietHDBanLe.GhiChu, SqlDbType.NVarChar, 200).Value = chitiethdbanle.GhiChu;
                cmd.Parameters.Add(Common.Constants.ChiTietHDBanLe.Deleted, SqlDbType.Bit).Value = chitiethdbanle.Deleted;
                rt = cmd.ExecuteNonQuery();
                cn.Close();
                return rt;
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return 0; }

        }

        public static int InsertUpdate(Entities.ChiTietHDBanle[] chitiethdbanle)
        {
            try
            {
                foreach (Entities.ChiTietHDBanle item in chitiethdbanle)
                {
                    if (InsertUpdate(item) == 0)
                    {
                        return 0;
                    }
                }
                return 1;
            }
            catch
            {
                return 0;
            }
        }
        #endregion
    }
}
#endregion